/********************************************************/
/* Probability of not being observed in the sample in t */
/********************************************************/
capture program drop miss
program define miss
version 11
syntax, occp(name)

use "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\select\unbalanced.dta", clear
gen orig=(ptreat==1 & `occp'==1 )
gen miss=(ptreat==1 & missing(F.sn))
keep if orig==1
collapse (mean) miss , by(ze an)
drop if an==2007
replace an=2007 if an==1999
replace an=1999 if an==1991
replace an=1991 if an==1982
replace an=1982 if an==1976
save p1.dta, replace

use "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\select\unbalanced.dta", clear
collapse dimm dimm75 Y_*, by(ze an)
joinby ze an using p1.dta, unm(m)
drop _merge

/* IV */
ivreg2 miss (dimm  = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store `occp'iv
tab miss
end

miss, occp(all) 
miss, occp(cad) 
miss, occp(tecemp2) 
miss, occp(bc) 
miss, occp(bcnq) 

/* IV */
esttab alliv  cadiv tecemp2iv bciv bcnqiv ///
using "C:\Users\Public\Documents\Tab21\TableA4.rtf" /// 
, append title("2SLS-CS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N rkf) star(* 0.10 ** 0.05 *** 0.01)

/* Analysis of attrition */
use "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\select\unbalanced.dta", clear
gen orig=(ptreat==1)
gen miss=(ptreat==1 & missing(F.sn))
keep if orig==1
drop if an==2007
gen lsnd=ln(sn/dp)
gen age2=age*age/100

/* sans covariates */
ivreg2 miss (dimm = dimm75) Y_an_*, partial(Y_an_*) cl(ze)
estimates store satall
ivreg2 miss (dimm = dimm75) Y_an_* if cad==1, partial(Y_an_*) cl(ze)
estimates store satcad
ivreg2 miss (dimm = dimm75) Y_an_* if tecemp2==1, partial(Y_an_*) cl(ze)
estimates store sattecemp2
ivreg2 miss (dimm = dimm75) Y_an_* if bc==1, partial(Y_an_*) cl(ze)
estimates store satbc
ivreg2 miss (dimm = dimm75) Y_an_* if bcnq==1, partial(Y_an_*) cl(ze)
estimates store satbcnq

/* Avec covariates */
ivreg2 miss (dimm = dimm75) fy lsnd age age2 trad ntrad lzepop  Y_an_*, partial(Y_an_*) cl(ze)
estimates store atall
ivreg2 miss (dimm = dimm75) fy lsnd age age2 trad ntrad lzepop  Y_an_* if cad==1, partial(Y_an_*) cl(ze)
estimates store atcad
ivreg2 miss (dimm = dimm75) fy lsnd age age2 trad ntrad lzepop  Y_an_* if tecemp2==1, partial(Y_an_*) cl(ze)
estimates store attecemp2
ivreg2 miss (dimm = dimm75) fy lsnd age age2 trad ntrad lzepop  Y_an_* if bc==1, partial(Y_an_*) cl(ze)
estimates store atbc
ivreg2 miss (dimm = dimm75) fy lsnd age age2 trad ntrad lzepop  Y_an_* if bcnq==1, partial(Y_an_*) cl(ze)
estimates store atbcnq

capture rm "C:\Users\Public\Documents\Tab21\TableA4.rtf"
esttab satall  satcad sattecemp2 satbc satbcnq ///
using "C:\Users\Public\Documents\Tab21\TableA4.rtf" /// 
, append title("2SLS-CS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 star(* 0.10 ** 0.05 *** 0.01)
 
esttab atall  atcad attecemp2 atbc atbcnq ///
using "C:\Users\Public\Documents\Tab21\TableA4.rtf" /// 
, append title("2SLS-CS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 star(* 0.10 ** 0.05 *** 0.01)
